import { getToken, setTime, setToken } from '@/utils/storage'
import { login, getInfo, getInfos } from '@/api/user'
export default {
  namespaced: true,
  state: {
    token: getToken(),
    userInfo: {}
  },
  mutations: {
    updateToken(state, payload) {
      state.token = payload
      setToken(payload)
    },
    updateUserInfo(state, payload) {
      state.userInfo = payload
    },
    delAll(state) {
      state.userInfo = {}
      state.token = ''
    }
  },
  actions: {
    async userLogin(context, payload) {
      const { data } = await login(payload)
      console.log(data)
      context.commit('updateToken', data.data)
      setTime()
    },
    async getUserInfo(context) {
      const { data } = await getInfo()
      console.log(data)
      const { data: data1 } = await getInfos(data.data.userId)
      context.commit('updateUserInfo', { ...data.data, ...data1.data })
    },
    logout(context) {
      context.commit('delAll')
    }
  }
}
